package tutorial.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import tutorial.service.IHelloWorldService;

@Controller
public class HelloWorldController {

	@Autowired
	IHelloWorldService helloWorldService;

	@RequestMapping(value = "/tutorial/helloWorld", method = RequestMethod.GET)
	public ModelAndView helloWorld() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("tutorial/helloWorld");
		mav.addObject("message", "Hello World!");
		return mav;
	}

	@RequestMapping(value = "/helloWorld2", method = RequestMethod.GET)
	@ResponseBody
	public String helloWorld2() {
		helloWorldService.getSysdate();
		return "Hello World";
	}

	@RequestMapping("/displayHeaderInfo.do")
	public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie,
			@RequestHeader("Accept-Encoding") String encoding,
			@RequestHeader("Keep-Alive") long keepAlive) {

		// ...

	}

	@ExceptionHandler(IOException.class)
	public String handleIOException(IOException ex, HttpServletRequest request) {
		return ClassUtils.getShortName(ex.getClass());
	}

}